package top.callback.inf.beautyleaf.server.web;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.callback.inf.beautyleaf.server.common.LeafResult;
import top.callback.inf.beautyleaf.server.model.UserModel;
import top.callback.inf.beautyleaf.server.service.UserService;

import java.util.Set;

/**
 * 用户接口
 *
 * @author Pengyu Gan
 * CreateDate 2021/3/30
 */
@Tag(name = "用户接口", description = "用户维护和角色绑定")
@RestController
@RequestMapping(value = "/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Operation(summary = "新增用户", parameters = {
            @Parameter(name = "userModel", description = "用户信息模型", required = true)
    })
    @PostMapping(value = "/add")
    public LeafResult<Boolean> addUser(@RequestBody UserModel userModel) {
        boolean success = userService.addUser(userModel);
        return LeafResult.success(success);
    }

    @Operation(summary = "编辑用户", parameters = {
            @Parameter(name = "userModel", description = "用户信息模型", required = true)
    })
    @PostMapping(value = "/edit")
    public LeafResult<Boolean> editUser(@RequestBody UserModel userModel) {
        boolean success = userService.editUser(userModel);
        return LeafResult.success(success);
    }

    @Operation(summary = "删除用户", parameters = {
            @Parameter(name = "userId", description = "用户 ID", required = true)
    })
    @GetMapping(value = "/remove/{userId}")
    public LeafResult<Boolean> removeUser(@PathVariable("userId") long userId) {
        boolean success = userService.removeUser(userId);
        return LeafResult.success(success);
    }

    @Operation(summary = "查询指定用户", parameters = {
            @Parameter(name = "userId", description = "用户 ID", required = true)
    })
    @GetMapping(value = "/get/{userId}")
    public LeafResult<UserModel> findById(@PathVariable("userId") long userId) {
        UserModel userModel = userService.findById(userId);
        return LeafResult.success(userModel);
    }

    @Operation(summary = "查询所有用户")
    @GetMapping(value = "/list")
    public LeafResult<Set<UserModel>> findAll() {
        Set<UserModel> users = userService.findAll();
        return LeafResult.success(users);
    }

}
